/*****************************************************************************
 * plum_hal.h
 *
 * Copyright (C) 2020 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

#ifndef __PLUM_HAL_H__
#define __PLUM_HAL_H__
/*****************************************************************************
 * Included Files
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

// HAL ID Group
#define PLUM_HAL_ID_VOL           0xFFFF
#define PLUM_HAL_ID_DEFAULT       0xFFFF
#define PLUM_HAL_IDX_SIMULAT_BASE 100

#define PLUM_HAL_ID_GPIO_A_BASE (0)
#define PLUM_HAL_ID_GPIO_B_BASE (1)
#define PLUM_HAL_ID_GPIO_C_BASE (2)
#define PLUM_HAL_ID_GPIO_D_BASE (3)
#define PLUM_HAL_ID_GPIO_E_BASE (4)
#define PLUM_HAL_ID_GPIO_F_BASE (5)
#define PLUM_HAL_ID_GPIO_G_BASE (6)
#define PLUM_HAL_ID_GPIO_H_BASE (7)
#define PLUM_HAL_ID_GPIO_I_BASE (8)
#define PLUM_HAL_ID_GPIO_J_BASE (9)
#define PLUM_HAL_ID_GPIO_K_BASE (10)

#define PLUM_HAL_ID_GPIO_BASE ((plum_u32)(0x00010000))
#define PLUM_HAL_ID_GPIO_END  (PLUM_HAL_ID_GPIO_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_UART_BASE ((plum_u32)(0x00020000))
#define PLUM_HAL_ID_UART_END  (PLUM_HAL_ID_UART_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_SPI_BASE ((plum_u32)(0x00030000))
#define PLUM_HAL_ID_SPI_END  (PLUM_HAL_ID_SPI_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_I2C_BASE ((plum_u32)(0x00040000))
#define PLUM_HAL_ID_I2C_END  (PLUM_HAL_ID_I2C_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_ADC_BASE ((plum_u32)(0x00050000))
#define PLUM_HAL_ID_ADC_END  (PLUM_HAL_ID_ADC_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_FLASH_BASE ((plum_u32)(0x00060000))
#define PLUM_HAL_ID_FLASH_END  (PLUM_HAL_ID_FLASH_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_ETH_BASE ((plum_u32)(0x00070000))
#define PLUM_HAL_ID_ETH_END  (PLUM_HAL_ID_ETH_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_TIME_BASE ((plum_u32)(0x00080000))
#define PLUM_HAL_ID_TIME_END  (PLUM_HAL_ID_TIME_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_DIO_BASE ((plum_u32)(0x00090000))
#define PLUM_HAL_ID_DIO_END  (PLUM_HAL_ID_DIO_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_CAN_BASE ((plum_u32)(0x000A0000))
#define PLUM_HAL_ID_CAN_END  (PLUM_HAL_ID_CAN_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_RTC_BASE ((plum_u32)(0x000B0000))
#define PLUM_HAL_ID_RTC_END  (PLUM_HAL_ID_RTC_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_I2S_BASE ((plum_u32)(0x000C0000))
#define PLUM_HAL_ID_I2S_END  (PLUM_HAL_ID_I2S_BASE + PLUM_HAL_ID_VOL)

#define PLUM_HAL_ID_PWM_BASE ((plum_u32)(0x000D0000))
#define PLUM_HAL_ID_PWM_END  (PLUM_HAL_ID_PWM_BASE + PLUM_HAL_ID_VOL)

// HAL Logic ID
#define PLUM_HAL_GPIO_ID(port, pin) \
    (PLUM_HAL_ID_GPIO_BASE + (PLUM_HAL_ID_GPIO_##port##_BASE << 8) + pin)
#define PLUM_HAL_GPIO_PORT(id) (((id)-PLUM_HAL_ID_GPIO_BASE) >> 8)
#define PLUM_HAL_GPIO_PIN(id)  (((id)-PLUM_HAL_ID_GPIO_BASE) & 0xFF)

#define PLUM_HAL_UART_ID(index) (PLUM_HAL_ID_UART_BASE + (index))
#define PLUM_HAL_UART_IDX(id)   ((id)-PLUM_HAL_ID_UART_BASE)

#define PLUM_HAL_SPI_ID(index) (PLUM_HAL_ID_SPI_BASE + (index))
#define PLUM_HAL_SPI_IDX(id)   ((id)-PLUM_HAL_ID_SPI_BASE)

#define PLUM_HAL_I2C_ID(index) (PLUM_HAL_ID_I2C_BASE + (index))
#define PLUM_HAL_I2C_IDX(id)   ((id)-PLUM_HAL_ID_I2C_BASE)

#define PLUM_HAL_ADC_ID(index) (PLUM_HAL_ID_ADC_BASE + (index))
#define PLUM_HAL_ADC_IDX(id)   ((id)-PLUM_HAL_ID_ADC_BASE)

#define PLUM_HAL_FLASH_ID(index) (PLUM_HAL_ID_FLASH_BASE + (index))
#define PLUM_HAL_FLASH_IDX(id)   ((id)-PLUM_HAL_ID_FLASH_BASE)

#define PLUM_HAL_ETH_ID(index) (PLUM_HAL_ID_ETH_BASE + (index))
#define PLUm_HAL_ETH_IDX(id)   ((id)-PLUM_HAL_ID_ETH_BASE)

#define PLUM_HAL_TIME_ID(index) (PLUM_HAL_ID_TIME_BASE + (index))
#define PLUM_HAL_TIME_IDX(id)   ((id)-PLUM_HAL_ID_TIME_BASE)

#define PLUM_HAL_DIO_ID(index) (PLUM_HAL_ID_DIO_BASE + (index))
#define PLUM_HAL_DIO_IDX(id)   ((id)-PLUM_HAL_ID_DIO_BASE)

#define PLUM_HAL_CAN_ID(index) (PLUM_HAL_ID_CAN_BASE + (index))
#define PLUM_HAL_CAN_IDX(id)   ((id)-PLUM_HAL_ID_CAN_BASE)

#define PLUM_HAL_RTC_ID(index) (PLUM_HAL_ID_RTC_BASE + (index))
#define PLUM_HAL_RTC_IDX(id)   ((id)-PLUM_HAL_ID_RTC_BASE)

#define PLUM_HAL_I2S_ID(index) (PLUM_HAL_ID_I2S_BASE + (index))
#define PLUM_HAL_I2S_IDX(id)   ((id)-PLUM_HAL_ID_I2S_BASE)

#define PLUM_HAL_PWM_ID(index) (PLUM_HAL_ID_PWM_BASE + (index))
#define PLUM_HAL_PWM_IDX(id)   ((id)-PLUM_HAL_ID_PWM_BASE)

// Judge
#define PLUM_HAL_ID_IS_GPIO(id) \
    (((id) >= PLUM_HAL_ID_GPIO_BASE) && ((id) <= PLUM_HAL_ID_GPIO_END))
#define PLUM_HAL_ID_IS_UART(id) \
    (((id) >= PLUM_HAL_ID_UART_BASE) && ((id) <= PLUM_HAL_ID_UART_END))
#define PLUM_HAL_ID_IS_SPI(id) \
    (((id) >= PLUM_HAL_ID_SPI_BASE) && ((id) <= PLUM_HAL_ID_SPI_END))
#define PLUM_HAL_ID_IS_I2C(id) \
    (((id) >= PLUM_HAL_ID_I2C_BASE) && ((id) <= PLUM_HAL_ID_I2C_END))
#define PLUM_HAL_ID_IS_ADC(id) \
    (((id) >= PLUM_HAL_ID_ADC_BASE) && ((id) <= PLUM_HAL_ID_ADC_END))
#define PLUM_HAL_ID_IS_FLASH(id) \
    (((id) >= PLUM_HAL_ID_FLASH_BASE) && ((id) <= PLUM_HAL_ID_FLASH_END))
#define PLUM_HAL_ID_IS_ETH(id) \
    (((id) >= PLUM_HAL_ID_ETH_BASE) && ((id) <= PLUM_HAL_ID_ETH_END))
#define PLUM_HAL_ID_IS_TIME(id) \
    (((id) >= PLUM_HAL_ID_TIME_BASE) && ((id) <= PLUM_HAL_ID_TIME_END))
#define PLUM_HAL_ID_IS_DIO(id) \
    (((id) >= PLUM_HAL_ID_DIO_BASE) && ((id) <= PLUM_HAL_ID_DIO_END))
#define PLUM_HAL_ID_IS_CAN(id) \
    (((id) >= PLUM_HAL_ID_CAN_BASE) && ((id) <= PLUM_HAL_ID_CAN_END))
#define PLUM_HAL_ID_IS_RTC(id) \
    (((id) >= PLUM_HAL_ID_RTC_BASE) && ((id) <= PLUM_HAL_ID_RTC_END))
#define PLUM_HAL_ID_IS_I2S(id) \
    (((id) >= PLUM_HAL_ID_I2S_BASE) && ((id) <= PLUM_HAL_ID_I2S_END))
#define PLUM_HAL_ID_IS_PWM(id) \
    (((id) >= PLUM_HAL_ID_PWM_BASE) && ((id) <= PLUM_HAL_ID_PWM_END))
// Invalid ID
#define PLUM_HAL_GPIO_INVALID_ID  (PLUM_HAL_ID_GPIO_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_UART_INVALID_ID  (PLUM_HAL_ID_UART_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_SPI_INVALID_ID   (PLUM_HAL_ID_SPI_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_I2C_INVALID_ID   (PLUM_HAL_ID_I2C_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_ADC_INVALID_ID   (PLUM_HAL_ID_ADC_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_FLASH_INVALID_ID (PLUM_HAL_ID_FLASH_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_ETH_INVALID_ID   (PLUM_HAL_ID_ETH_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_TIME_INVALID_ID  (PLUM_HAL_ID_TIME_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_DIO_INVALID_ID   (PLUM_HAL_ID_DIO_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_CAN_INVALID_ID   (PLUM_HAL_ID_CAN_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_RTC_INVALID_ID   (PLUM_HAL_ID_RTC_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_I2S_INVALID_ID   (PLUM_HAL_ID_I2S_BASE + PLUM_HAL_ID_DEFAULT)
#define PLUM_HAL_PWM_INVALID_ID   (PLUM_HAL_ID_PWM_BASE + PLUM_HAL_ID_DEFAULT)

/*****************************************************************************
 * Public Types
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* __PLUM_HAL_H__ */
/****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
